from django.db import models

# Create your models here.


#写商品的思路：首先应该要写分类，就把所有商品归类
from utils.modelsMixin import ModelSetMixin

#def __str__返回数据的意义就是在序列化器在页面展示的时候，页面在下拉页面可以展示出来你返回的数据
class GoodsType(ModelSetMixin):
    name = models.CharField(max_length=40,verbose_name='分类名')
    parent = models.ForeignKey('self',on_delete=models.SET_NULL,blank=True,null=True,verbose_name='上级分类')#关联自己
    class Meta:
        db_table = 'goodstype'
        verbose_name = '商品分类'
        verbose_name_plural = verbose_name
    def __str__(self):
        return f'%s.%s' %(self.id,self.name)



#其次再写商品的信息的模型

# models.DecimalField(max_digits=None, decimal_places=None[, **options])
# 使用 Decimal 实例表示固定精度的十进制数的字段。它有两个必须的参数：
# max_digits：数字允许的最大位数
# decimal_places：小数的最大位数


class Commodity(ModelSetMixin):
    STATUS_CHOICES = (
        (0, '未发布'),
        (1, '发布')
    )
    name = models.CharField(max_length=100, verbose_name='商品名')
    caption = models.CharField(max_length=140, verbose_name='副标题', null=True)
    brand = models.CharField(max_length=40, verbose_name='品牌', null=True, blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    stock = models.IntegerField(verbose_name='库存')
    sales = models.IntegerField(verbose_name='销量', default=0)
    comments = models.IntegerField(default=0, verbose_name='评价数')
    classification1 = models.ForeignKey(GoodsType, on_delete=models.PROTECT, verbose_name='一级分类',
                                        related_name='classification1')
    classification2 = models.ForeignKey(GoodsType, on_delete=models.PROTECT, verbose_name='二级分类',
                                        related_name='classification2')
    pack = models.TextField(verbose_name='包装信息', null=True, blank=True)
    serviceafter_sale = models.TextField(verbose_name='售后服务', null=True, blank=True)
    status = models.IntegerField(default=0, verbose_name='状态', choices=STATUS_CHOICES)

    class Meta:
        ordering = ['-sales', '-comments', '-create_time']
        db_table = 'commodity'
        verbose_name = '商品'
        verbose_name_plural = verbose_name


# 商品图片
class CommodityImg(models.Model):
    img_url = models.TextField(verbose_name='图片地址')
    priority = models.IntegerField(default=0, verbose_name='优先级')
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='商品')

    class Meta:
        ordering = ['-priority', '-id']
        db_table = 'commodity_img'
        verbose_name = '商品图'
        verbose_name_plural = verbose_name
